package com.cf.game.stage;

import com.cf.Event;
import com.cf.core.player.Player;
import com.cf.core.stage.impl.AbsNamPartitionMatchStage;
import com.cf.game.GameContext;
import com.cf.game.constant.SysConstant;
import com.cf.game.service.pojo.H5Game;
import com.cf.game.service.service.UserInfoService;
import com.cf.game.service.util.BeanFactoryUtil;
import com.cf.serialize.common.RechargeResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * game-server-MatchStage
 * 匹配尝尽
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/7 下午 03:00
 */
@Slf4j
public class MatchStage extends AbsNamPartitionMatchStage {



    public MatchStage(int heartBeatStep, int stageTime, String stageNum, int partitionNum) {
        super(heartBeatStep, stageTime, stageNum,partitionNum);

    }

    @Override
    protected void goToGameStage(List<Player> partition) {
        while (partition.size() < partitionNum) {
            partition.add(null);
        }
        ClassRoom classRoom = new ClassRoom(SysConstant.gameTime, partition.get(0), partition.get(1));
    }

    @Override
    public void getMatchTicketAndJoinQueue(Player player) {
        H5Game h5Game = BeanFactoryUtil.getBean(GameContext.class).getH5Game();
        boolean chargingSuccess = BeanFactoryUtil.getBean(UserInfoService.class).charging(player.getId(), h5Game.getMoney().doubleValue(), h5Game.getName() + "扣费");
        if (chargingSuccess) {
            //加入到匹配队列
            this.matchPlayers.add(player);
            if (this.matchPlayers.size() >= SysConstant.partitionNum) {
                matching();
            }
        } else {
            player.sendMessage(new RechargeResponse());
        }
    }

    @Override
    protected void handleEvent(Event event) {
        log.info("匹配场景只需要处理TicketEvent，其他的事件概不处理。。。。。。。。");
    }
}
